package com.los.rabbitmq.tx;

import com.los.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @program: rabbitmq
 * @description:事务的模式
 * 缺点：降低了消息的吞吐量
 * @author: Yujuan
 * @create: 2018-08-02 16:27
 **/
public class TxSend {
    private static final String EXCHANGE_NAME="los_exchange_topic";
    private static final String QUEUE_NAME="los_queue_tx";

    public static void main(String[] args) throws IOException, TimeoutException {
        //获取一个连接
        Connection connection= ConnectionUtils.getConnection();
        //创建一个通道
        Channel channel=connection.createChannel();
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);

        String msg="Hello tx message";

        try {
            channel.txSelect();
            channel.basicPublish("",QUEUE_NAME,null,msg.getBytes());
            System.out.println("---Send:"+msg);
            int x=1/0;
            channel.txCommit();
        }catch (Exception e){
            channel.txRollback();
            System.out.println("send message txRollback");
        }
        channel.close();
        connection.close();
    }
}
